package com.training1.core.common.schedule;


import java.io.File;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LockDataBaseAop{

	private static final Logger log = LoggerFactory.getLogger(LockDataBaseAop.class);
	
	@Value("${lock.file.path}")
	private String lockFilePath;
	
	@Around("@annotation(com.training1.core.common.schedule.LockDataBase)")
	public Object doAround(ProceedingJoinPoint pjd) throws Throwable {
		
		File file = new File(lockFilePath);
		if(file.exists()){
			log.warn("锁文件存在");
			return null;
		}
		if(!file.createNewFile()){
			log.warn("锁文件存在");
			return null;
		}
		
		try{
		
		Object[] args = pjd.getArgs();
		Object ret = null;
		if(args.length>0){
			ret = pjd.proceed(pjd.getArgs());
		}else{
			ret = pjd.proceed();
		}
		return ret;
		}catch(Exception e){
//			log.warn("批处理加文件锁失败:",e);
			throw e;
		}finally{
			file.delete();
		}
		
	
	}

}
